package com.qf.web.servlet;

import com.qf.domain.*;
import com.qf.service.CartService;
import com.qf.service.OrderService;
import com.qf.service.UserService;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrderServiceImpl;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/*
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *            佛曰:
 *                   写字楼里写字间，写字间里程序员；
 *                   程序人员写程序，又拿程序换酒钱。
 *                   酒醒只在网上坐，酒醉还来网下眠；
 *                   酒醉酒醒日复日，网上网下年复年。
 *                   但愿老死电脑间，不愿鞠躬老板前；
 *                   奔驰宝马贵者趣，公交自行程序员。
 *                   别人笑我忒疯癫，我笑自己命太贱；
 *                   不见满街漂亮妹，哪个归得程序员？
 */
@WebServlet(name = "OrderServlet",value = "/orderservlet")
public class OrderServlet extends BaseServlet {

    public String getOrderView(HttpServletRequest request, HttpServletResponse response) throws Exception{
        User user = (User) request.getSession().getAttribute("user");
        //1获取购买的商品
        CartService cartService=new CartServiceImpl();
        List<CartItem> cartList = cartService.findCart(user.getId());
        //2获取收获地址
        try {
            UserService userService=new UserServiceImpl();
            List<Address> addList=userService.findAddress(user.getId());

            //3放入request域中
            request.setAttribute("cartList", cartList);
            request.setAttribute("addList", addList);
            return "/order.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "结账失败...");
            return "/message.jsp";
        }
    }

    public String addOrder(HttpServletRequest request,HttpServletResponse response) throws Exception{
        User user = (User) request.getSession().getAttribute("user");
        String aid = request.getParameter("aid");
        if(StringUtils.empty(aid)){
            request.setAttribute("msg", "提交订单失败,地址为空");
            return "/message.jsp";
        }
        //查询购物车

        CartService cartService=new CartServiceImpl();
        List<CartItem> cartList = cartService.findCart(user.getId());
        OrderService orderService=new OrderServiceImpl();
        try {
            if(cartList!=null&&cartList.size()>0) {
                Order order=orderService.addOrder(user.getId(), Integer.parseInt(aid), cartList);
                request.setAttribute("order",order);
                return "/orderSuccess.jsp";
            }else{
                request.setAttribute("msg","购物车为空" );
                return "/message.jsp";
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg","提交订单失败" );
            return "/message.jsp";
        }

    }


    public String getOrderList(HttpServletRequest request,HttpServletResponse response) throws Exception{
        User user = (User) request.getSession().getAttribute("user");

        OrderService orderService=new OrderServiceImpl();
        try {
            List<Order> orderList=orderService.findByUid(user.getId());
            request.setAttribute("orderList", orderList);
            return "/orderList.jsp";
        } catch (Exception e){
            request.setAttribute("msg", "查询订单失败");
            return "/message.jsp";
        }

    }

    public String getOrderDetail(HttpServletRequest request,HttpServletResponse response) throws  Exception{
        User user = (User) request.getSession().getAttribute("user");
        String oid = request.getParameter("oid");
        OrderService orderService=new OrderServiceImpl();
        try {
            //查询订单
            Order order=orderService.findByOid(oid);
            System.out.println(order.toString());
            //查询订单祥
            List<OrderDetail> orderDetails=orderService.findOrderDetailByOid(oid);

            request.setAttribute("order", order);
            request.setAttribute("orderDetails", orderDetails);

            return "/orderDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询订单详情失败");
            return "/message.jsp";
        }
    }

}
